﻿package
{
	import flash.display.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.*;
	public class ControlPanel extends Sprite
	{
		private var playbutton: PlaButton;
		private var resetbutton: ResetButton;
		private var stepforwardbutton: StepForwardButton;
		private var _stepforward: Boolean = false;
		private var stepbackbutton: StepBackButton
		private var _stepback: Boolean = false;
		private var _pause:Boolean = true;
		private var _reset:Boolean = false;
	 	private var scale:Number;
		private var matrix:Matrix;
		private var _height:Number=100;
		private var _width:Number=100;

		public function ControlPanel(height:Number=100, width:Number=100)
		{
			_height = height;
			_width = width;
			init();
		}
		public function init():void
		{
			//First, add the control panel area
			var panel:Sprite = new Sprite();
			addChild(panel);
			matrix = new Matrix;
			matrix.createGradientBox(_width, 0.1*_height, Math.PI/2,  0, 0.9*_height);
			panel.graphics.beginGradientFill(GradientType.LINEAR, [0xeeeeee, 0xcccccc], [1,1], [0,255], matrix);
			panel.graphics.lineStyle(0, 0x000000, 0);
			panel.graphics.moveTo(0, 0.9*_height);
			panel.graphics.lineTo(_width,0.9*_height);
			panel.graphics.lineTo(_width, 0.98*_height);
			panel.graphics.curveTo(_width, _height, 0.99*_width, _height);
			panel.graphics.lineTo(0.01*_width, _height);
			panel.graphics.curveTo(0, _height, 0, 0.98*_height);
			panel.graphics.lineTo(0, 0.9*_height)
			panel.graphics.endFill();
			panel.graphics.lineStyle(0, 0x000000, 1);
			trace(_height,_width);
			panel.x = 0;
			panel.y = 0;

			playbutton = new PlaButton();
			addChild(playbutton);
			playbutton.y = 0.91*_height;
			playbutton.x = 0.01*_width;
			scale = .1*_height;

			playbutton.draw(scale, _pause, true, false);
			// The parameters are: scale, _pause, button up, button red. _pause true is the playbutton,
			//_pause false the pause button.

			//addEventListener(Event.ENTER_FRAME, onEnterFrame);
			playbutton.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverPlay);
			playbutton.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutPlay);
			playbutton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownPlay);
			playbutton.addEventListener(MouseEvent.MOUSE_UP, onMouseUpPlay);

			resetbutton = new ResetButton();
			addChild(resetbutton);
			resetbutton.x = playbutton.x + 0.95*scale;
			resetbutton.y = playbutton.y;
			resetbutton.draw(scale, true, false);
			// the parameters are scale, up, red.


			resetbutton.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverReset);
			resetbutton.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutReset);
			resetbutton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownReset);
			resetbutton.addEventListener(MouseEvent.MOUSE_UP, onMouseUpReset);
			
			stepbackbutton = new StepBackButton();
			addChild(stepbackbutton);
			stepbackbutton.x = resetbutton.x + 1*scale;
			stepbackbutton.y = playbutton.y;

			stepbackbutton.draw(scale, true, false);
			// The parameters are: scale, _pause, button up, button red. _pause true is the stepbackbutton,
			//_pause false the pause button.

			//addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stepbackbutton.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverstepback);
			stepbackbutton.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutstepback);
			stepbackbutton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownstepback);
			stepbackbutton.addEventListener(MouseEvent.MOUSE_UP, onMouseUpstepback);

			stepforwardbutton = new StepForwardButton();
			addChild(stepforwardbutton);
			stepforwardbutton.x = stepbackbutton.x + .8*scale;
			stepforwardbutton.y = playbutton.y;

			stepforwardbutton.draw(scale, true, false);
			// The parameters are: scale, _pause, button up, button red. _pause true is the stepforwardbutton,
			//_pause false the pause button.

			//addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stepforwardbutton.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverstepforward);
			stepforwardbutton.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutstepforward);
			stepforwardbutton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownstepforward);
			stepforwardbutton.addEventListener(MouseEvent.MOUSE_UP, onMouseUpstepforward);

		}

		//Mouse functions for Play button:

		private function onMouseOverPlay(event:MouseEvent):void
		{

			playbutton.draw(scale, _pause, true, true);

		}
		private function onMouseOutPlay(event:MouseEvent):void
		{

			playbutton.draw(scale, _pause, true, false);

		}
		private function onMouseDownPlay(event:MouseEvent):void
		{

			playbutton.draw(scale, _pause, false, true);

		}
		private function onMouseUpPlay(event:MouseEvent):void
		{
			if(_pause)
			{
			_pause = false;
			playbutton.draw(scale, _pause, true, true);
			}
			else
			{
			_pause = true;
			playbutton.draw(scale, _pause, true, true);
			}
		}

		//Mouse functions for Reset button:

		private function onMouseOverReset(event:MouseEvent):void
		{
			resetbutton.draw(scale, true, true);
		}
		private function onMouseOutReset(event:MouseEvent):void
		{
			resetbutton.draw(scale, true, false);
		}

		private function onMouseUpReset(event:MouseEvent):void
		{
			resetbutton.draw(scale, true, true);
			_reset = false;
		}
		private function onMouseDownReset(event:MouseEvent):void
		{
			resetbutton.draw(scale, false, true);
			_reset = true;
			_pause = false;
			playbutton.draw(scale, _pause, true, true);
		}
		
		//Mouse functions for stepback button:

		private function onMouseOverstepback(event:MouseEvent):void
		{
			stepbackbutton.draw(scale, true, true);
		}
		private function onMouseOutstepback(event:MouseEvent):void
		{
			stepbackbutton.draw(scale, true, false);
		}

		private function onMouseUpstepback(event:MouseEvent):void
		{
			stepbackbutton.draw(scale, true, true);
			_stepback = false;
		}
		private function onMouseDownstepback(event:MouseEvent):void
		{
			stepbackbutton.draw(scale, false, true);
			_pause = true;
			_stepback = true;
			playbutton.draw(scale, _pause, true, true);
		}
		
		//Mouse functions for stepforward button:

		private function onMouseOverstepforward(event:MouseEvent):void
		{
			stepforwardbutton.draw(scale, true, true);
		}
		private function onMouseOutstepforward(event:MouseEvent):void
		{
			stepforwardbutton.draw(scale, true, false);
		}

		private function onMouseUpstepforward(event:MouseEvent):void
		{
			stepforwardbutton.draw(scale, true, true);
			_stepforward = false;
		}
		private function onMouseDownstepforward(event:MouseEvent):void
		{
			stepforwardbutton.draw(scale, false, true);
			_pause = true;
			_stepforward = true;
			playbutton.draw(scale, _pause, true, true);
		}
		
// functions to read and set _pause and _reset
		public function get pause():Boolean
		{
			return _pause;
		}
		public function set pause(n:Boolean):void
		{
			_pause = n;
			playbutton.draw(scale, _pause, true, false);
		}
		public function get reset():Boolean
		{
			return _reset;
		}

		public function get stepforward():Boolean
		{
			return _stepforward;
		}
		
		public function get stepback():Boolean
		{
			return _stepback;
		}
}
}